/*
 * @Author: 吴世扬 18368095041@163.com
 * @Date: 2024-07-16 15:24:36
 * @LastEditors: 吴世扬 18368095041@163.com
 * @LastEditTime: 2024-08-19 16:16:54
 * @FilePath: /vue3-uniapp/src/components/ui/PageContainer/useNotify.ts
 */
/*
 * @Author: 吴世扬 18368095041@163.com
 * @Date: 2024-07-16 15:24:36
 * @LastEditors: 吴世扬 18368095041@163.com
 * @LastEditTime: 2024-07-16 15:57:06
 * @FilePath: /vue3-uniapp/src/components/ui/PageContainer/useNotify.ts
 */
import type { TnNotifyInstance } from '@tuniao/tnui-vue3-uniapp';

import { useNotifySubject } from '@/service/app/useNotifySubject';
import { getCurrentRoute } from '@/utils/route';
import { useCurrentPageHeightChange } from '@/components/ui/FixedTop/useCurrentPageHeightChange';

export const useNotify = (notifyRef: Ref<TnNotifyInstance | undefined>) => {
    const { onNotify, dispose } = useNotifySubject();
    const offsetHeight = ref(0);

    useCurrentPageHeightChange((height) => {
        offsetHeight.value = height;
    });

    onMounted(() => {
        onNotify((option) => {
            if (!notifyRef.value)
                throw new Error('[PageContainer]notifyRef未初始化');
            notifyRef.value?.show(option);
        });

        // TODO: 后续注意事件销毁
        uni.$uv.route.onBeforeRouteLeave(({ from }) => {
            const currentRoute = getCurrentRoute();
            if (from.indexOf(currentRoute) !== -1) {
                dispose();
                console.log(`[${from}]notifyRef释放`);
            }
        });
    });

    return {
        offsetHeight
    };
};
